#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1100, K = 13, M = 1 << K;

int n, m;
int dp[2][N][M];
int g[1010];
vector<int> state;
int cnt[M];

bool check(int u)
{
    for (int i = 0; i < m; i ++)
         if((u >> i & 1) == 1 && ((u >> i + 1 & 1) == 1 || (u >> i + 2 & 1) == 1))
            return false;
    return true;
}


int count(int state) {
    int res = 0;
    for (int i = 0; i < m; i++)
        if (state >> i & 1)
            res++;
    return res;
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < m; j++) {
            char c;
            cin >> c;
            g[i] += (c == 'H') << j;
        }

    for (int i = 0; i < 1 << m; i ++ )
        if (check(i))
        {
            state.push_back(i);
            cnt[i] = count(i);
        }
    
    for (int i = 1; i <= n + 1; i ++ )
        for (auto& a : state)
            for (auto& b : state)
                for (auto& c : state)
                {
                    if (a & b | a & c | b & c) continue;
                    if (g[i] & b | g[i - 1] & a) continue;
                    dp[i & 1][a][b] = max(dp[i & 1][a][b], dp[i - 1 & 1][c][a] + cnt[b]);
                }
    
    int res = 0;
    for (auto& a : state)
        for (auto& b : state)
            res = max(res, dp[n & 1][a][b]);

    cout << res << endl;

    return 0;
}
